package org.jdemo.util;

import java.math.BigDecimal;
import java.util.Random;

/**
 * RandomUtil
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2013-9-16
 * @since V1.0
 */
public class RandomUtil {
	private static final Random RANDOM = new Random();
	private static final char[] NUMBERS = "0123456789".toCharArray();
	private static final char[] LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static final char[] NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    /**
     * 随机选取一个
     * @param arr
     * @param <T>
     * @return
     */
    public static <T> T randomOne(T... arr) {
        return arr[randomInt(0, arr.length - 1)];
    }

	/**
	 * 产生length位的随机字符串
     *
	 * @param length 字符长度
	 * @return
	 */
	public static String randomStr(int length) {
		if (length < 1) {
			return null;
		}
		
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = NUMBERS_AND_LETTERS[RANDOM.nextInt(72)];
		}
		
		return new String(randBuffer);
	}

    /**
     * 产生minLength到maxLength长度的字符串
     *
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return
     */
	public static String randomStr(int minLength, int maxLength) {
		return randomStr(randomInt(minLength, maxLength));
	}

    /**
     * 产生length长度的字母字符
     *
     * @param length 字符长度
     * @return
     */
	public static String randomLetter(int length) {
		if (length < 1) {
			return null;
		}
		
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = LETTERS[RANDOM.nextInt(51)];
		}
		
		return new String(randBuffer);
	}

    /**
     * 产生minLengh到maxLength长度的字母字符
     *
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return
     */
	public static String randomLetter(int minLength, int maxLength) {
		return randomLetter(randomInt(minLength, maxLength));
	}
	
	/**
	 * 产生length位的随机数字
     *
	 * @param length
	 * @return
	 */
	public static String randomNum(int length) {
		if (length < 1) {
			return null;
		}
		
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = NUMBERS[RANDOM.nextInt(10)];
			if(i == 0) {
				if(randBuffer[i] == '0') {
					i--;
				}
			}
		}
		
		return new String(randBuffer);
	}

    /**
     * 随即产生min到max之间的随机数,包含max和min
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
	public static int randomInt(int min, int max) {
		if(min == max){
			return min;
		}
		return RANDOM.nextInt(max + 1) % (max - min + 1) + min;
	}

    /**
     * 随机产生true或false的值
     *
     * @return
     */
	public static boolean randomBoolean() {
		return RANDOM.nextInt(2) % 2 == 0;
	}

    /**
     * 随机产生min到max之间的long值
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
	public static long randomLong(long min, long max) {
		if(min == max){
			return min;
		}
		return Math.abs(RANDOM.nextLong() % (max - min + 1)) + min;
	}

    /**
     * 随机产生min到max之间的float值
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
	public static float randomFloat(int min, int max) {
		if(min == max){
			return min;
		}
		return RANDOM.nextFloat() * (max - min + 1) + min;
	}

    /**
     * 随机产生min到max之间的double值
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
	public static double randomDouble(int min, int max) {
		if(min == max){
			return 1.0 * min;
		}
		return RANDOM.nextDouble() * (max - min + 1) + min;
	}

    /**
     * 随机产生min到max之间的bigDecimal值
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
	public static BigDecimal randomBigDecimal(int min, int max) {
		return new BigDecimal(randomDouble(min, max));
	}

    /**
     * 随机产生字符串数组
     * @param count
     * @param min
     * @param max
     * @return
     */
    public static String[] randomStrArray(int count, int min, int max) {
        String[] arr = new String[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomStr(min, max);
        }
        return arr;
    }

    /**
     * 随机产生数字串数组
     * @param count
     * @param min
     * @param max
     * @return
     */
    public static Integer[] randomIntArray(int count, int min, int max) {
        Integer[] arr = new Integer[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomInt(min, max);
        }
        return arr;
    }

    /**
     * 随机产生数字串数组
     * @param count
     * @param min
     * @param max
     * @return
     */
    public static Long[] randomLongArray(int count, long min, long max) {
        Long[] arr = new Long[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomLong(min, max);
        }
        return arr;
    }

    /**
     * 随机产生数字串数组
     * @param count
     * @param min
     * @param max
     * @return
     */
    public static Float[] randomFloatArray(int count, int min, int max) {
        Float[] arr = new Float[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomFloat(min, max);
        }
        return arr;
    }

    /**
     * 随机产生数字串数组
     * @param count
     * @param min
     * @param max
     * @return
     */
    public static Double[] randomDoubleArray(int count, int min, int max) {
        Double[] arr = new Double[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomDouble(min, max);
        }
        return arr;
    }

    /**
     * 随机产生数字串数组
     * @param count
     * @return
     */
    public static Boolean[] randomBooleanArray(int count) {
        Boolean[] arr = new Boolean[count];
        for (int i = 0; i < count; i++) {
            arr[i] = randomBoolean();
        }
        return arr;
    }
}
